<?php

namespace app\controller;

use app\BaseController;
use app\fe\amis\Crud;
use think\helper\Str;

class UIBuilderController extends BaseController
{
    public function table($modelName)
    {
        $modelClass = '\\app\\model\\' . ucfirst($modelName);

        $model = new $modelClass(); /** @var \think\Model $model */
        $fields = $model->db()->getFields();

        $view = new Crud();
        $view->api = url('/lc/data/table/' . $modelName)->suffix(false)->build();

        // TODO 调整 DAO 层和 ViewModel 层分工，然后从 ViewModel 取相关数据
        $view->columns = [];
        foreach ($fields as $field) {
            $view->columns[] = [
                'name' => $field['name'],
                'label' => Str::title(str_replace('_', ' ', $field['name'])),
            ];
        }

        return $this->toJson(0, $view);
    }

}